/******************************************************************
 * Created by: Goh Chee Hong                                       
 * Date of Creation: 1/Jan/2009     
 * 
 *                                                                
 * Description: The form that allows the user to send an instant message
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pbdms.BusinessLogic;
using System.Threading;
using Pbdms.BusinessLogic.FileTransfer;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Tools
{
    public partial class FrmInstantMsg : newForms
    {
        delegate void delegateWorker(object sender, string args);
        string keyid;
        public FrmInstantMsg()
        {
            InitializeComponent();
            this.Resize += new EventHandler(FrmInstantMsg_Resize);
            Pbdms.Common.Global.paintBackGround(this);
        }

        void FrmInstantMsg_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                Program.mainForm.DunFlashWindowEx(this);
            }
        }
        /// <summary>
        /// set key id
        /// </summary>
        /// <param name="key"></param>
        public void setID(string key)
        {
            keyid = key;
           
        }
        /// <summary>
        /// set title
        /// </summary>
        public void setTitle(){
            Common.DataObject.Buddy buddy = Program.friendControl.getAFriendByKeyID(keyid);
            this.Text=buddy.Name+"'s converation with you";
        }

        /// <summary>
        /// change name
        /// </summary>
        /// <param name="keyID"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public override void changeName(string keyID, string oldName, string newName)
        {
            
        }
        /// <summary>
        /// get key id
        /// </summary>
        /// <returns></returns>
        public string getID()
        {
            return keyid;
        }
        /// <summary>
        /// send an instant message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            
           
            
            
            Common.DataObject.Buddy buddy = Program.friendControl.getAFriendByKeyID(keyid);
            
            if (!(richmsg.Text.Equals(""))){
                
                if(buddy!=null){
                    if(buddy.IpAdd!=null){

                        lock (this)
                        {
                            try
                            {
                                //choose the first one
                                richName.Text = "You says:\n";
                                richName.SelectionStart = 0;
                                richName.SelectionLength = richName.Text.Length;
                                //choose the second one
                                richExtra.Rtf = richmsg.Rtf;
                                richExtra.SelectionStart = 0;
                                richExtra.SelectionLength = richExtra.Text.Length;
                                //choose the third one
                                richDisplay.SelectionStart = richDisplay.Text.Length;
                                richDisplay.SelectionLength = 0;
                                //richExtra2.SelectionLength = richExtra2.Text.Length;
                                richDisplay.SelectedRtf = richName.SelectedRtf;
                               
                                //richExtra2.SelectedText="\r\n";
                                richDisplay.SelectionStart = richDisplay.Text.Length;
                                richDisplay.SelectionLength = 0;
                                richDisplay.SelectedRtf = richExtra.SelectedRtf;
                                richDisplay.SelectionStart = richDisplay.Text.Length;
                                richDisplay.SelectionLength = 0;
                                richDisplay.SelectedText = "\r\n\n";
                                richDisplay.SelectionStart = richDisplay.Text.Length;
                                richDisplay.ScrollToCaret();
                                richDisplay.Refresh();
                                richName.SelectionStart=0;
                                richName.SelectionLength=0;
                                richExtra.SelectionStart = 0;
                                richExtra.SelectionLength = 0;
                                
                            }
                            catch
                            {

                            }
                        }
                        BusinessLogic.InstantMessaging.InstantMessager instantMessage = new Pbdms.BusinessLogic.InstantMessaging.InstantMessager(richmsg.Rtf, buddy.KeyID, Program.networkCtrller);
                        instantMessage.FailedToSend += new Pbdms.BusinessLogic.InstantMessaging.InstantMessager.FailedToSendHandler(instantMessage_FailedToSend);
                        Thread thThread= new Thread(new ThreadStart(instantMessage.sendMessage));
                        thThread.IsBackground=true;
                        thThread.Start();
                    }
                }
            }           
        }
        /// <summary>
        /// falied to send
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="message"></param>
        void instantMessage_FailedToSend(object sender, string message)
        {
            if (this.InvokeRequired)
            {
                delegateWorker deleWorker = new delegateWorker(instantMessage_FailedToSend);
                this.BeginInvoke(deleWorker, new object[] { sender,message });
            }else
            {
               
           
                    lock (this)
                    {

                        try
                        {
                            //choose the first one
                            richName.Text = "Failed to deliver the following message\n";
                            richName.SelectAll();
                            richName.SelectionColor = Color.Red;
                            richName.SelectionStart = 0;
                            richName.SelectionLength = richName.Text.Length;
                            //choose the second one
                            richExtra.Rtf = message;
                            richExtra.SelectionStart = 0;
                            richExtra.SelectionLength = richExtra.Text.Length;
                            //choose the third one
                            richDisplay.SelectionStart = richDisplay.Text.Length;
                            richDisplay.SelectionLength = 0;
                            //richExtra2.SelectionLength = richExtra2.Text.Length;
                            richDisplay.SelectedRtf = richName.SelectedRtf;

                            //richExtra2.SelectedText="\r\n";
                            richDisplay.SelectionStart = richDisplay.Text.Length;
                            richDisplay.SelectionLength = 0;
                            richDisplay.SelectedRtf = richExtra.SelectedRtf;
                            richDisplay.SelectionStart = richDisplay.Text.Length;
                            richDisplay.SelectionLength = 0;
                            richDisplay.SelectedText = "\r\n\n";
                            richDisplay.SelectionStart = richDisplay.Text.Length;
                            richDisplay.ScrollToCaret();
                            richDisplay.Refresh();
                           
                            richExtra.SelectionStart = 0;
                            richExtra.SelectionLength = 0;
                            
                            richName.SelectionStart = 0;
                            richName.SelectionLength = richName.Text.Length;
                            richName.SelectionColor = Color.Black;
                            richName.SelectionStart = 0;
                            richName.SelectionLength = 0;
                        }
                        catch
                        {

                        }

                    }
                
            }
        }
        /// <summary>
        /// received message
        /// </summary>
        /// <param name="msg"></param>
        public void MessageReceived(string msg)
        {
            Common.DataObject.Buddy buddy = Program.friendControl.getAFriendByKeyID(keyid);
            if (buddy != null)
            {
                lock (this)
                {
                    
                    try
                    {
                        //choose the first one
                        richName.Text = buddy.Name+" says:\n";
                        richName.SelectionStart = 0;
                        richName.SelectionLength = richName.Text.Length;
                        //choose the second one
                        richExtra.Rtf =msg;
                        richExtra.SelectionStart = 0;
                        richExtra.SelectionLength = richExtra.Text.Length;
                        //choose the third one
                        richDisplay.SelectionStart = richDisplay.Text.Length;
                        richDisplay.SelectionLength = 0;
                        //richExtra2.SelectionLength = richExtra2.Text.Length;
                        richDisplay.SelectedRtf = richName.SelectedRtf;

                        //richExtra2.SelectedText="\r\n";
                        richDisplay.SelectionStart = richDisplay.Text.Length;
                        richDisplay.SelectionLength = 0;
                        richDisplay.SelectedRtf = richExtra.SelectedRtf;
                        richDisplay.SelectionStart = richDisplay.Text.Length;
                        richDisplay.SelectionLength = 0;
                        richDisplay.SelectedText = "\r\n\n";
                        richDisplay.SelectionStart = richDisplay.Text.Length;
                        richDisplay.ScrollToCaret();
                        richDisplay.Refresh();
                        richName.SelectionStart = 0;
                        richName.SelectionLength = 0;
                        richExtra.SelectionStart = 0;
                        richExtra.SelectionLength = 0;   
                    }
                    catch
                    {

                    }
                        
                }
            }
            
        }


       
       

        private void FontToolStripButton_Click(object sender, EventArgs e)
        {
            if (FontDlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                richmsg.SelectionFont = FontDlg.Font;
            }
        }


        private void FontColorToolStripButton_Click(object sender, EventArgs e)
        {
            if (ColorDlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                richmsg.SelectionColor = ColorDlg.Color;
            }
        }

        private void BoldToolStripButton_Click(object sender, EventArgs e)
        {
            Font currentFont = richmsg.SelectionFont;
            FontStyle fs = currentFont.Style;
            if (richmsg.SelectionFont.Bold == true)
            {
                fs = fs & ~FontStyle.Bold;

            }
            else
            {
                fs = fs | FontStyle.Bold;
            }

            richmsg.SelectionFont =
              new Font(currentFont.FontFamily, currentFont.Size, fs);

            // Check/Uncheck Bold button
            BoldToolStripButton.Checked = (richmsg.SelectionFont.Bold ? true : false);
        }

        private void UnderlineToolStripButton_Click(object sender, EventArgs e)
        {
            Font currentFont = richmsg.SelectionFont;
            FontStyle fs = currentFont.Style;
            if (richmsg.SelectionFont.Underline == true)
            {
                fs = fs & ~FontStyle.Underline;

            }
            else
            {
                fs = fs | FontStyle.Underline;
            }

            richmsg.SelectionFont =
              new Font(currentFont.FontFamily, currentFont.Size, fs);

            // Check/Uncheck Bold button
            UnderlineToolStripButton.Checked = (richmsg.SelectionFont.Underline ? true : false);
        }

        private void btnSend_Click_1(object sender, EventArgs e)
        {

        }

        private void FontToolStripButton_Click_1(object sender, EventArgs e)
        {

        }

        private void FontColorToolStripButton_Click_1(object sender, EventArgs e)
        {

        }

        private void BoldToolStripButton_Click_1(object sender, EventArgs e)
        {

        }

        private void UnderlineToolStripButton_Click_1(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// send a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDia.ShowDialog();
           
    
            string filelocation = null;
            if (result == DialogResult.OK)
            {

                filelocation = openFileDia.FileName;
                try
                {
                   
                    Common.DataObject.Buddy buddy = Program.friendControl.getAFriendByKeyID(keyid);
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filelocation);
                    FileTransferSenderGui fileTransfer = new FileTransferSenderGui(keyid,buddy.Name,fileInfo,Program.networkCtrller);
                       
             
                      
                    Program.mainForm.frmFileTransfer.MdiParent = Program.mainForm;
                    Program.mainForm.frmFileTransfer.addControl(fileTransfer);
                    Program.mainForm.frmFileTransfer.Activate();

                    Program.mainForm.frmFileTransfer.Show();
                
                }
                catch
                {
                    MessageBox.Show("An unknown error occurred in sending a file");
                }
                
            }
        }
    }
}